package com.iot.device.utils;

import com.iot.common.core.domain.entity.SysDept;
import com.iot.common.core.redis.RedisCache;
import com.iot.common.utils.SecurityUtils;
import com.iot.common.utils.StringUtils;
import com.iot.common.utils.spring.SpringUtils;
import com.iot.system.service.ISysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysDeptUtils {

    private static final Logger logger = LoggerFactory.getLogger(SysDeptUtils.class);

    /**
     * 根据ID获取
     * @param deptId
     * @return
     */
    public static SysDept getSysDept(Long deptId){
        Map<String, SysDept> allSysDept = getAllSysDeptMap();
        for(Map.Entry<String, SysDept> entry : allSysDept.entrySet()){
            if(entry.getKey().startsWith(deptId + ":")){
                return entry.getValue();
            }
        }
        ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        if(sysDept != null){
            List<SysDept> sysDeptList = sysDeptService.selectAllDeptList();
            ClientRedisUtils.updateRedisSysDept(sysDeptList);
            return sysDept;
        }
        sysDept = new SysDept();
        sysDept.setDelFlag(false);
        sysDept.setDeptId(deptId);
        sysDept.setParentId(1L);
        sysDept.setDeptName("系统自动创建");
        sysDept.setLongitude(0.00F);
        sysDept.setLatitude(0.00F);
        sysDept.setStatus("1");
        sysDept.setDelFlag(false);
        sysDeptService.insertDept(sysDept);
        List<SysDept> sysDeptList = sysDeptService.selectAllDeptList();
        ClientRedisUtils.updateRedisSysDept(sysDeptList);
        logger.warn("未查询到deptId为 {} 的部门数据，创建默认部门信息...");

        return sysDept;
    }

    public static String getSysDeptName(Long deptId) {
        SysDept sysDept = getSysDept(deptId);
        return sysDept == null ? null : sysDept.getDeptName();
    }

    public static Map<String, SysDept> getAllSysDeptMap() {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        return redisCache.getCacheMap(ClientRedisUtils.EQ_SYS_DEPT);
    }

    public static Map<Long, SysDept> getAllSysDept() {
        Map<Long, SysDept> sysDeptMap = new HashMap<>();
        Map<String, SysDept> allSysDeptMap = getAllSysDeptMap();
        for(SysDept sysDept : allSysDeptMap.values()){
            sysDeptMap.put(sysDept.getDeptId(), sysDept);
        }
        return sysDeptMap;
    }

    public static List<Long> getDownDeptIds(Long deptId){
        ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
        List<Long> deptIds = sysDeptService.selectFullDeptIdListById(deptId);
        return deptIds;
    }

    public static List<Long> getUpDeptIds(Long deptId){
        SysDept sysDept = getSysDept(deptId);
        List<Long> deptIds = new ArrayList<>();
        deptIds.add(deptId);
        String[] split = sysDept.getAncestors().split(",");
        for(String str : split){
            if(StringUtils.isNotEmpty(str)){
                deptIds.add(Long.parseLong(str));
            }
        }
        return deptIds;
    }

    public static SysDept getSysDept() {
        Long deptId = SecurityUtils.getDeptId();
        return getSysDept(deptId);
    }
}
